﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Convertor.Interface;
using Wpf.CommonTools.StaticExtension;
using Wpf.CommonTools.SystemExtension;

namespace Convertor
{
    /// <summary>
    /// MovePluginToWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MovePluginToWindow : Window
    {
        private IPluginsItemCollection rootPlugins; //根插件列表
        private ObservableCollection<IPluginItem> currentPlugins; //当前级别插件      
        private ObservableCollection<IPluginItem> parentPlugins; //要移动的项目的父插件
        private IPluginItem toMovePlugin; //要移动的插件
        private IPluginItem currentParentPlugin; //当前父插件
        private PopForm p;

        public MovePluginToWindow(IPluginsItemCollection rootPlugins, IPluginItem toMoveItem)
        {
            this.rootPlugins = rootPlugins;
            this.toMovePlugin = toMoveItem;
            this.currentParentPlugin = toMoveItem.ParentPlugin;
            this.parentPlugins = this.currentPlugins = toMoveItem.ParentPlugin == null ? rootPlugins.SelectedPlugins : toMoveItem.ParentPlugin.ChildPlugins;

            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            p = new PopForm(this);
            lstSeletedPlugin.ItemsSource = this.currentPlugins;
            initControl();
        }

        private bool close;
        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (Config.GlobalConfig.EnableEscToCloseChildWindows && this.IsActive && e.Key == Key.Escape)
            {
                close = true;
            }
        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            if (close && Config.GlobalConfig.EnableEscToCloseChildWindows && this.IsActive && e.Key == Key.Escape)
            {
                this.Close();
            }
        }

        private void Window_Activated(object sender, EventArgs e)
        {
            //lstSeletedPlugin.ItemsSource = this.currentPlugins;
            //initControl();
        }

        private void Button_OK_Click(object sender, RoutedEventArgs e)
        {
            bool b = this.parentPlugins.Remove(toMovePlugin);
            if (!b)
            {
                p.PopAlertMessage(UILanguage.FindResource("UnexceptedError"));
                return;
            }
            initControl();
            this.currentPlugins.Add(toMovePlugin);
            toMovePlugin.ParentPlugin = currentParentPlugin;

            this.DialogResult = true;
            this.Close();
        }

        private void Button_Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void Button_Click_Up(object sender, RoutedEventArgs e)
        {
            if (this.currentParentPlugin != null)
            {
                this.currentParentPlugin = this.currentParentPlugin.ParentPlugin;

                this.currentPlugins = this.currentParentPlugin == null ? rootPlugins.SelectedPlugins : this.currentParentPlugin.ChildPlugins;

                initControl();
            }
        }

        private void lstSeletedPlugin_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            IPluginItem item = lstSeletedPlugin.SelectedItem as IPluginItem;
            if (item != null)
            {
                this.currentParentPlugin = item;
                this.currentPlugins = item.ChildPlugins;
                initControl();
            }
        }

        private void initControl()
        {
            btUp.IsEnabled = this.currentParentPlugin != null;
            lstSeletedPlugin.ItemsSource = this.currentPlugins.Where(p => p != toMovePlugin);
        }
    }
}
